package berkeley.lab6;

/* BankApp.java */
import java.io.*;

/**
 * A bank application. Allows a user to create and manipulate banking accounts, using an ATM that is shared by all
 * banking applications.
 */
public class BankApp {

    private BufferedReader bReader =
            new BufferedReader(new InputStreamReader(System.in));
    private VirtualTeller ATM = new VirtualTeller();

    public static void main(String[] args) {
        greeting();
        usage();
        BankApp bankApp = new BankApp();

        try {
            String command = bankApp.readLine("--> ");
            while (!command.equals("quit")) {
                try {
                    if (command.equals("open")) {
                        bankApp.open();
                    } else if (command.equals("deposit")) {
                        bankApp.doDeposit();
                    } else if (command.equals("withdraw")) {
                        bankApp.doWithdraw();
                    } else if (command.equals("inquire")) {
                        bankApp.doInquire();
                    } else {
                        System.err.println("Invalid command: " + command);
                        usage();
                    }
                } catch (BadAccountException e) {
                    System.out.println(e.getMessage());
                } catch (BadTransactionException e) {
                    System.out.println(e.getMessage());
                }
                command = bankApp.readLine("--> ");
            }
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    public BankApp() {
        // The field declarations have initializers;
        //   no initialization is needed here.
    }

    /**
     * open() prompts the user to create an account and creates one in the ATM.
     *
     * @exception IOException if there are problems reading user input.
     */
    private void open() throws IOException {
        String name = readLine("Enter name: ");
        int newNum = ATM.openAccount(name);

        System.out.println(name + ", your new account number is: " + newNum);
        System.out.println("Thanks for opening an account with us!");
    }

    /**
     * doDeposit() prompts the user for an account number and tries to perform a deposit transaction on that account.
     *
     * @exception IOException if there are problems reading user input.
     */
    private void doDeposit() throws IOException, BadAccountException, BadTransactionException {
        // Get account number.
        int acctNumber = readInt("Enter account number: ");
        int amount = readInt("Enter amount to deposit: ");

        ATM.deposit(acctNumber, amount);
        System.out.println("New balance for #" + acctNumber + " is "
                + ATM.balanceInquiry(acctNumber));
    }

    /**
     * doWithdraw() prompts the user for an account number and tries to perform a withdrawal transaction from that
     * account.
     *
     * @exception IOException if there are problems reading user input.
     */
    private void doWithdraw() throws IOException, BadAccountException, BadTransactionException {
        // Get account number.
        int acctNumber = readInt("Enter account number: ");
        int amount = readInt("Enter amount to withdraw: ");

        ATM.withdraw(acctNumber, amount);
        System.out.println("New balance for #" + acctNumber + " is "
                + ATM.balanceInquiry(acctNumber));
    }

    /**
     * doInquire() prompts the user for an account number, then attempts to discover and print that account's balance.
     *
     * @exception IOException if there are problems reading user input.
     */
    private void doInquire() throws IOException, BadAccountException {
        int acctNumber = readInt("Enter account number: ");

        System.out.println("Balance for #" + acctNumber + " is "
                + ATM.balanceInquiry(acctNumber));
    }

    /**
     * greeting() displays a greeting message on the screen.
     */
    private static void greeting() {
        System.out.println("-------------------");
        System.out.println("Welcome to the bank");
        System.out.println("-------------------");
        System.out.println();
    }

    /**
     * usage() displays instructions on using the command line arguments.
     */
    private static void usage() {
        System.out.println("Valid commands are: "
                + "open, deposit, withdraw, inquire, quit");
    }

    /**
     * readLine() prints the given prompt and returns a string from the input stream.
     *
     * @param prompt is the string printed to prompt the user.
     */
    private String readLine(String prompt) throws IOException {
        System.out.print(prompt);
        System.out.flush();
        return bReader.readLine();
    }

    /**
     * readInt() returns an integer from the input stream after prompting the user.
     *
     * @param prompt is the string printed to prompt the user.
     * @return an int read from the user.
     */
    private int readInt(String prompt) throws IOException {
        String text = readLine(prompt);
        return Integer.valueOf(text).intValue();
    }
}
